<?php
/*
 * 排泄状态信息编辑及展示接口
 * */
class Apiv1_ExcretionController extends Ctrl_Apiatk{
    //编辑    
    public function editAction(){
        $p = $_REQUEST;
        $pId = empty($p['sid'])?Tool_Fnc::ajaxMsg('老人ID不能为空'):Tool_Fnc::safe_string($p['sid']);
        $pBigstatus = empty($p['big_status'])?'':Tool_Fnc::safe_string($p['big_status']);
        $pSmallstatus = empty($p['small_status'])?'':Tool_Fnc::safe_string($p['small_status']);
        $pUpd = empty($p['upd'])?'':Tool_Fnc::safe_string($p['upd']);

        $pAt = empty($p['at'])?Tool_Fnc::ajaxMsg('时间点不能为空'):Tool_Fnc::safe_string($p['at']);

        $tSMO = new SeniorModel;
        $tId = $tSMO->field('id')->where('id = '.$pId)->fRow()['id'];
        if(empty($tId)){Tool_Fnc::ajaxMsg('该老人不存在');}

        $tEMO = new ExcretionModel;
        $tTime = time();
        $tEData = $tEMO->field('id,small_times,big_times,upd')->where('sid = '.$pId.' and date = "'.date('Y-m-d',$tTime).'" and at = '.$pAt)->fRow(); 
        if(empty($tEData)){//记录为空，添加操作
            if(!empty($pSmallstatus) && empty($pBigstatus)){//小便
                $tData = array(
                    'small_times' => 1,
                    'small_status' => $pSmallstatus,
                    'upd' => $pUpd,
                    'sid' => $pId,
                    'created' => $tTime,
                    'date' => date('Y-m-d',$tTime), 
                    'at' => $pAt, 
                );
                if(!$tId = $tEMO->insert($tData)){
                    Tool_Fnc::ajaxMsg('编辑失败 001');
                }
            }else if(!empty($pBigstatus) && empty($pSmallstatus)){//大便
                $tData = array(
                    'big_times' => 1,
                    'big_status' => $pBigstatus,
                    'sid' => $pId,
                    'created' => $tTime,
                    'date' => date('Y-m-d',$tTime),
                    'at' => $pAt, 
                );
                if(!$tId = $tEMO->insert($tData)){
                    Tool_Fnc::ajaxMsg('编辑失败 002');
                }
            }else{
                $tData = array(
                    'big_times' => 1,
                    'small_times' => 1,
                    'big_status' => $pBigstatus,
                    'small_status' => $pSmallstatus,
                    'upd' => $pUpd,
                    'created' => $tTime,
                    'sid' => $pId,
                    'date' => date('Y-m-d',$tTime),
                    'at' => $pAt, 
                );
                if(!$tId = $tEMO->insert($tData)){
                    Tool_Fnc::ajaxMsg('编辑失败 003');
                }
            }  
        }else{//记录存在，修改操作
            if(!empty($pSmallstatus) && empty($pBigstatus)){//小便
                $tData = array(
                    'small_times' => $tEData['small_times']+1,
                    'upd' => $tEData['upd']+$pUpd,
                    'small_status' => $pSmallstatus,
                    'sid' => $pId,
                    'created' => $tTime,
                    'date' => date('Y-m-d',$tTime),
                    'at' => $pAt, 
                    'id' => $tEData['id'],
                );
                if(!$tEMO->update($tData)){
                    Tool_Fnc::ajaxMsg('编辑失败 004');
                }
            }else if(!empty($pBigstatus) && empty($pSmallstatus)){//大便
                $tData = array(
                    'big_times' => $tEData['big_times']+1,
                    'big_status' => $pBigstatus,
                    'sid' => $pId,
                    'created' => $tTime,
                    'date' => date('Y-m-d',$tTime),
                    'at' => $pAt, 
                    'id' => $tEData['id'],    
                );
                if(!$tEMO->update($tData)){
                    Tool_Fnc::ajaxMsg('编辑失败 005');
                }
            }else{
                $tData = array(
                    'small_times' => $tEData['small_times']+1,
                    'big_times' => $tEData['big_times']+1,
                    'upd' => $tEData['upd']+$pUpd,
                    'small_status' => $pSmallstatus,
                    'big_status' => $pBigstatus,
                    'sid' => $pId,
                    'created' => $tTime,
                    'date' => date('Y-m-d',$tTime),
                    'at' => $pAt, 
                    'id' => $tEData['id'],
                );
                if(!$tEMO->update($tData)){
                    Tool_Fnc::ajaxMsg('编辑失败 006');
                }
            }
        }
        Tool_Fnc::ajaxMsg('编辑成功',1);
    }
    //展示
    public function listAction(){
        $p = $_REQUEST;
        $pId = empty($p['sid'])?Tool_Fnc::ajaxMsg('老人ID不能为空'):Tool_Fnc::safe_string($p['sid']);
        $pDate = empty($p['date'])?'':Tool_Fnc::safe_string($p['date']);
        $pAt = empty($p['at'])?Tool_Fnc::ajaxMsg('时间点不能为空'):Tool_Fnc::safe_string($p['at']);
        $tSMO = new SeniorModel;
        $tId = $tSMO->field('id')->where('id = '.$pId)->fRow()['id'];
        if(empty($tId)){Tool_Fnc::ajaxMsg('该老人不存在');}

        $tEMO = new ExcretionModel;
        //默认查询当天数据
        $time = time();
        $tWhere = ' sid = '.$pId.' and date = "'.date('Y-m-d',$time).'" and at = '.$pAt;
        if(!empty($pDate)){
           $tWhere = ' sid = '.$pId.' and date = "'.$pDate.'" and at = '.$pAt; 
        }
        $tDatas = $tEMO->field('id,sid,small_status,small_times,big_status,big_times,upd')->where($tWhere)->order('id desc')->fList();
        if(!empty($tDatas)){
            Tool_Fnc::ajaxMsg('',1,$tDatas);
        }
    }
    //获取周几
    private function getTimeWeek($time, $i = 0) {
        $weekarray = array('1', '2', '3', "4", "5", "6", "7");
        $oneD = 24 * 60 * 60;
        return $weekarray[date("w", $time + $oneD * ($i-1))];
    }
}
?>
